
#include    <iostream>

#include    "jell/exception.hpp"

using namespace jell;

void    baz(void)
{
    THROW(0, "baz msg");
}

void    bar(void)
{
    TRY("bar msg", baz());
}

void    foo(void)
{
    TRY("foo msg", bar());
}

int main(void)
{
    std::cerr << "step 11111\n";
    try {
        baz();
    }
    catch(JellException& ex) {
        std::cerr << ex << std::endl;
    }
    catch(...) {
        std::cerr << "unknown exception.";
    }

    std::cerr << "step 22222\n";
    try {
        try {
            baz();
        }
        catch (JellException &e) {
            THROW_EX(e.code(), e, "step--22");
        }
        catch (std::bad_alloc &e) {
            THROW(EXTERNAL, "bad alloc " "step-22");
        }
        catch (std::exception &e) {
            THROW_FMT(EXTERNAL, "External exception " "step-22" ": %s",  e.what());
        }
    }
    catch(JellException& ex) {
        std::cerr << "step 22222 catch JellException.\n";
        std::cerr << ex << std::endl;
    }
    catch(...) {
        std::cerr << "step 22222 catch something.\n";
        std::cerr << "unknown exception.";
    }



    std::cerr << "step 33333\n";
    try {
        TRY("main msg", baz());
    }
    catch(JellException& ex) {
        std::cerr << "step 33333 catch JellException.\n";
        std::cerr << ex << std::endl;
    }
    catch(...) {
        std::cerr << "step 33333 catch something.\n";
        std::cerr << "unknown exception.";
    }

    std::cerr << "step 33333322222\n";
    try {
        try {
            bar();
        }
        catch (JellException &e) {
            THROW_EX(e.code(), e, "step--33333322");
        }
        catch (std::bad_alloc &e) {
            THROW(EXTERNAL, "bad alloc " "step-33333322");
        }
        catch (std::exception &e) {
            THROW_FMT(EXTERNAL, "External exception " "step-33333322" ": %s",  e.what());
        }
    }
    catch(JellException& ex) {
        std::cerr << "step 33333322222 catch JellException.\n";
        std::cerr << ex << std::endl;
    }
    catch(...) {
        std::cerr << "step 33333322222 catch something.\n";
        std::cerr << "unknown exception.";
    }


    std::cerr << "step 44444\n";
    try {
        TRY("main msg", bar());
    }
    catch(JellException& ex) {
        std::cerr << "step 44444 catch JellException.\n";
        std::cerr << ex << std::endl;
    }
    catch(...) {
        std::cerr << "step 44444 catch something.\n";
        std::cerr << "unknown exception.";
    }

    std::cerr << "step 55555\n";
    try {
        TRY("main msg", foo());
    }
    catch(JellException& ex) {
        std::cerr << ex << std::endl;
    }

    return  0;
}

